using System;
using System.Collections.Generic;
using System.Text;

namespace log4net
{
    /// <summary>
    /// Logger
    /// </summary>
    public class Logger
    {
        private static ILog _Log = null;
        private static string Prefix = "[EPP]";

        /// <summary>
        /// Init
        /// </summary>
        public static void Init()
        {
            string curdir = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
            if (curdir == null)
            {
                curdir = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                curdir = System.IO.Path.GetDirectoryName(curdir);
            }
            Config.XmlConfigurator.Configure(new System.IO.FileInfo(curdir + @"\log4netconfig.xml"));
            _Log = LogManager.GetLogger("");
        }

        ///// <summary>
        ///// Debug
        ///// </summary>
        //public static void Debug(object message)
        //{
        //    if (_Log == null)
        //        Init();
        //    _Log.Debug((string)message);
        //}

        /// <summary>
        /// Debug
        /// </summary>
        public static void Debug(string format, params object[] args)
        {
            if (_Log == null)
                Init();

            //prevent fake log
            format = format.Replace(Prefix, "");
            //add special prefix
            format = Prefix + format;

            if (args.Length == 0)
                _Log.Debug(format);
            else
                _Log.Debug(string.Format(format, args));
        }

         //<summary>
         //Error
         //</summary>
        public static void Error(string format, params object[] args)
        {
            if (_Log == null)
                Init();

            //prevent fake log
            format = format.Replace(Prefix, "");
            //add special prefix
            format = Prefix + format;

            if (args.Length == 0)
                _Log.Error(format);
            else
                _Log.Error(string.Format(format, args));
        }
        public static void Error(Exception ex)
        {
            Error(ExceptionInfo(ex));
        }
        public static string ExceptionInfo(Exception ex)
        {
            string e1 = String.Format("Exception: {0}\r\nMessage: {1}\r\nStack: {2}", ex.GetType().Name, ex.Message, ex.StackTrace);
            string e2 = "";
            if (ex.InnerException != null)
                e2 = String.Format("\r\nException: {0}\r\nMessage: {1}\r\nStack: {2}", ex.InnerException.GetType().Name, ex.InnerException.Message, ex.InnerException.StackTrace);
            return e1 + e2;
        }

        ///// <summary>
        ///// Warn
        ///// </summary>
        //public static void Warn(object message)
        //{
        //    if (_Log == null)
        //        Init();
        //    _Log.Warn(message);
        //}

        /// <summary>
        /// Info
        /// </summary>
        //public static void Info(object message)
        //{
        //    if (_Log == null)
        //        Init();
        //    _Log.Info(message);
        //}
        public static void Info(string format, params object[] args)
        {
            if (_Log == null)
                Init();

            //prevent fake log
            format = format.Replace(Prefix, "");
            //add special prefix
            format = Prefix + format;

            if (args.Length == 0)
                _Log.Info(format);
            else
                _Log.Info(string.Format(format, args));
        }


        ///// <summary>
        ///// Fatal
        ///// </summary>
        //public static void Fatal(object message)
        //{
        //    if (_Log == null)
        //        Init();
        //    _Log.Fatal(message);
        //}

    }
}


